﻿using StarPizza.Model.BaseModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.Model.Core
{
    public class CashRegister
    {
        StarDataDataContext db;
        public event Action<int> CashChanged;
        public CashRegister(StarDataDataContext db)
        {
            this.db = db;
            if (!db.Financial.Any())
            {
                db.Financial.InsertOnSubmit(new Financial
                {
                    Cash = 1000000
                });
                Utility.TrySubmitChanges(db);
            }
        }
        public int Cash
        {
            get 
            {
                return db.Financial.First().Cash;
            }
            set
            {
                if (db.Financial.First().Cash != value)
                {
                    db.Financial.First().Cash = value;
                    Utility.TrySubmitChanges(db);
                    if (CashChanged != null)
                        CashChanged(value);
                }
            }
        }
    }
}
